#include "stm32f030x8.h"
#include "keyscan.h"
#include "uart.h"
#include "gpio.h"
#include "config.h"
#include "orbital.h"
#include "common.h"
#include "ab_set.h"
#include "stm32f0xx_ll_gpio.h"
#include "stm32f0xx_ll_bus.h"
#include "stm32f0xx_hal_pwr.h"

//#define KeyUp		LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_4)//
//#define KeyDown		LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_3)//ADCIN4
//#define KeyLeft		LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_1)//ADCIN1
//#define KeyRight	LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_2)//ADCIN0
//#define KeyOk		LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_0)//ADCIN2
//#define Bootooth_disconnect		GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4)//ADCIN2
//#define RETURN_KEY1	GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10)
//#define RETURN_KEY2	GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)

uint16_t scan_cycle=0;
uint8_t key_final_status=0;

uint8_t keypowr_status=0;
uint8_t keyOK_status=0;
uint8_t keyLeft_status=0;
uint8_t keyRight_status=0;
uint16_t continues = 0;
uint16_t press_time = 0;
uint8_t powers_off = 0;

uint8_t key_debounce = 0;

void set_press_time(void)
{
	press_time = 2001;
}

unsigned char key_Handle(void)
{
	unsigned char p_keyup=0;
	unsigned char p_keyOK=0;
	unsigned char p_keyleft=0;
	unsigned char p_keright=0;
	
	//p_keyup = ~(KeyPowr|0xfe);
	p_keyOK = ~(KeyOk|0xfe);
	p_keyleft = ~(KeyLeft|0xfe);
	p_keright = ~(KeyRight|0xfe);
	return (p_keyup | (p_keyOK<<1) | (p_keyleft<<2) | (p_keright<<3));
	
}

void keyscan(void)
{
	uint8_t key_status=0;
//	static uint8_t bt_flag_back=0xff;
	
	if(scan_cycle)
		return;
	scan_cycle = 4;//20;
	
	key_status = key_Handle();

		
	if(key_status != key_final_status)
	{
		key_debounce++;
		if(key_debounce < 3)return;

		key_debounce = 0;
		key_final_status = key_status;
	//	String_Printf(USART_2, &key_final_status, 1);

		///////////////////////////////////////////////////////////////////
		if((key_final_status & KEY_LEFT_MASK) != keyLeft_status)
		{
			keyLeft_status = key_final_status & KEY_LEFT_MASK;
		//	if((key_final_status&KEY_RIGHT_MASK) || (key_final_status&KEY_LEFT_MASK) || (key_final_status&KEY_DOWN_MASK))return;
			if(keyLeft_status)
			{
				//if(Con_Var.key_mode)return;
				
				charge_dir_dispose(0xff);
				GoBack_Dispose();
			}
			
		}
		///////////////////////////////////////////////////////////////////////
		if((key_final_status & KEY_RIGHT_MASK) != keyRight_status)
		{
			keyRight_status = key_final_status & KEY_RIGHT_MASK;
		//	if((key_final_status&KEY_RIGHT_MASK) || (key_final_status&KEY_UP_MASK) || (key_final_status&KEY_LEFT_MASK))return;
			
			if(keyRight_status)
			{
			//	Con_Var.orbital_dir = ~Con_Var.orbital_dir;
			//if(Con_Var.key_mode)return;
				charge_dir_dispose(0);
				GoBack_Dispose();
				//video_start(yun_start);
			}
		//	remotoe_cursor_dispose(keydown_status);
		}

		if((key_final_status & KEY_OK_MASK) != keyOK_status)
		{
			keyOK_status = key_final_status & KEY_OK_MASK;
			if(keyOK_status)
			{
				OK_key_start();
				StopMtion_Start(0xff);
			}
		}
	//	String_Printf(USART_1,&key_final_status,1);
//////////////////////////////////////////////////////////////////	
	}
	
}


void scan_cycle_count(void)
{
	if(scan_cycle)scan_cycle--;
	if(continues>1)continues--;
	if(press_time>1)press_time--;
}

void set_power_on_int(uint8_t tatus)
{
	powers_off = tatus;
}

uint8_t return_powers(void)
{
	return powers_off;
}


void Enter_Sleep_Mode(void)
{
	//system_clock_init(RCC_SYSCLK_Div1);
	exti_init_beforEnterStop(1);
	keypowr_status = 0;
	key_final_status = 0;
	LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
	HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
	//LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_PWR);
	SystemClock_Config();
	exti_init_beforEnterStop(0);		
//	__disable_irq();
//		NVIC_SystemReset();
}

void enter_test_mode(void)
{	
/*	uint32_t ptime = 0;
	LED_ON;
	if(key_Handle()&KEY_POWR_MASK)
	{
		//scan_cycle = 4;
		for(ptime=0;ptime <8000000;ptime++)
		{}
		if(key_Handle()&KEY_POWR_MASK)
		{
			FW_test_main();
		}
	}*/
}

